package com.healthcloud.controller.modules.patient;

import com.healthcloud.entity.HlDoctorEntity;
import com.healthcloud.entity.questionnaire.*;
import com.healthcloud.service.HlDoctorService;
import com.healthcloud.service.questionnaire.QuestionnaireService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.ShiroUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问卷调查
 *
 * @author zj
 */
@Controller
@RequestMapping("questionnaire")
public class QuestionnaireController {

    @Autowired
    private QuestionnaireService questionnaireService;

    @Autowired
    private HlDoctorService hlDoctorService;

    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd(Model model, String idCard, Long titleId) {
        model.addAttribute("idCard", idCard);
        model.addAttribute("titleId", titleId);
        return "bus/questionnaire/addQuestionnaire";
    }

    @RequestMapping(value = "/toShow", method = RequestMethod.GET)
    public String toShow(Model model, Long titleId, Long recordId, Long typeid, String idcard, String patientId, Long id) {
        model.addAttribute("titleId", titleId);
        model.addAttribute("recordId", recordId);
        model.addAttribute("typeid", typeid);
        model.addAttribute("idcard", idcard);
        model.addAttribute("patientId", patientId);
        model.addAttribute("id", id);
        return "bus/questionnaire/showQuestionnaire";
    }

    @ApiOperation(value = "获取问卷列表", notes = "title:问卷名 sort:排序")
    @RequestMapping(value = "/titles", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes titles(@ApiParam(name = "type", value = "1:血压 2:血糖 3:血脂 4:骨松")
                          @RequestParam(value = "type") Long type) {
        QuestionnaireTitleEntity title = new QuestionnaireTitleEntity();
        title.setType(type);
        List<QuestionnaireTitleEntity> titles = questionnaireService.queryListTitle(title, null);
        return new ApiRes(titles);
    }

    @ApiOperation(value = "获取子问卷列表", notes = "subtitle:子标题 sort:排序")
    @RequestMapping(value = "/subtitles", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes subtitles(@ApiParam(name = "titleId", value = "问卷id")
                             @RequestParam(value = "titleId") Long titleId) {
        QuestionnaireSubtitleEntity subtitle = new QuestionnaireSubtitleEntity();
        subtitle.setTitleId(titleId);
        List<QuestionnaireSubtitleEntity> titles = questionnaireService.queryListSubtitle(subtitle, null);
        return new ApiRes(titles);
    }

    @ApiOperation(value = "获取问题列表", notes = "question:问题 type:1:单选 2:多待 state:1:必填 2:可以为空 sort:排序" +
            "options:选项 {text:选项名 option:值 sort:排序}")
    @RequestMapping(value = "/questions", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes questions(@ApiParam(name = "subtitleId", value = "子问卷id")
                             @RequestParam(value = "subtitleId") Long subtitleId) {
        QuestionnaireQuestionEntity question = new QuestionnaireQuestionEntity();
        question.setSubtitleId(subtitleId);
        List<QuestionnaireQuestionVo> titles = questionnaireService.queryListQuestionVo(question, null);
        return new ApiRes(titles);
    }

    @ApiOperation(value = "跳转到病人列表页", notes = "type:1:血压 2:血糖 3:血脂 4:骨松")
    @RequestMapping(value = "/toPatients", method = RequestMethod.GET)
    public String toPatients(@ApiParam(name = "type", value = "1:血压 2:血糖 3:血脂 4:骨松")
                             @RequestParam(value = "type") Integer type, Model model) {
        model.addAttribute("type", type);
        return "bus/questionnaire/patients";
    }

    @ApiOperation(value = "跳转到记录列表", notes = "")
    @RequestMapping(value = "/toRecords", method = RequestMethod.GET)
    public String toRecords(@ApiParam(name = "idCard", value = "病人id")
                            @RequestParam(value = "idCard") String idCard,
                            @ApiParam(name = "type", value = "1:血压 2:血糖 3:血脂 4:骨松")
                            @RequestParam(value = "type") Integer type, Model model) {
        model.addAttribute("idCard", idCard);
        model.addAttribute("type", type);
        return "bus/questionnaire/records";
    }

    @ApiOperation(value = "问卷记录列表", notes = "title:问卷题目 completeTime:完成时间 patient:病人姓名 doctorName:医生姓名")
    @RequestMapping(value = "/records", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes records(@ApiParam(name = "idCard", value = "身份证")
                           @RequestParam(value = "idCard") String idCard,
                          @ApiParam(name = "type", value = "1:血压 2:血糖 3:血脂 4:骨松")
                           @RequestParam(value = "type") Integer type, PageParam pageParam) {
        QuestionnaireRecordVo recordVo = new QuestionnaireRecordVo();
        recordVo.setIdCard(idCard);
        recordVo.setType(type);
        List<QuestionnaireRecordVo> records = questionnaireService.queryListRecordVo(recordVo, pageParam);
        int count = questionnaireService.queryTotalRecordVo(recordVo);

        Map<String, Object> map = new HashMap();
        map.put("dataList", records);
        map.put("iTotalRecords", count);
        map.put("iTotalDisplayRecords", count);
        map.put("sEcho", pageParam.getsEcho());
        return new ApiRes(map);
    }

    @ApiOperation(value = "保存问卷")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes add(@RequestBody QuestionnaireRecordVo recordVo) {
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
        if (doctor != null) {
            recordVo.setDoctorId(doctor.getId());
        }
        questionnaireService.add(recordVo);
        return ApiRes.SUCCESS;
    }

    /*
    @ApiOperation(value = "获取问卷结果")
    @RequestMapping(value = "/results", method = RequestMethod.POST)
    @ResponseBody
    public JsonMsg results(QuestionnaireResultVo resultVo) {
        List<QuestionnaireResultEntity> results = questionnaireService.queryListResult(resultVo, null);
        return new JsonMsg(results);
    }
    */

    @ApiOperation(value = "获取问卷结果")
    @RequestMapping(value = "/results", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes results(
            @ApiParam(name = "titleId", value = "问卷id") @RequestParam(value = "recordId") String recordId,
            @ApiParam(name = "subtitleId", value = "子问卷id") @RequestParam(value = "subtitleId") Long subtitleId
    ) {
        QuestionnaireResultVo resultVo = new QuestionnaireResultVo();
        resultVo.setSubtitleId(subtitleId);
        resultVo.setRecordId(Long.valueOf(recordId));
        List<QuestionnaireResultEntity> results = questionnaireService.queryListResult(resultVo, null);
        return new ApiRes(results);
    }

}
